//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DatedOISRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/OvernightIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (DateTime startDate, DateTime endDate, Cephei::QL::IQuote^ fixedRate, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex) : CRateHelper(CDatedOISRateHelper::typeid)
{
    CQuote^ _CfixedRate;
    COvernightIndex^ _CovernightIndex;
    try
    {
#ifdef HANDLE
        _phDatedOISRateHelper = NULL;
#endif
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate);
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate);
        _CfixedRate = safe_cast<CQuote^> (fixedRate);
        _CfixedRate->Lock();
        Handle<QuantLib::Quote>& _fixedRate = static_cast<Handle<QuantLib::Quote>&> (_CfixedRate->GetHandle ()); 
        _CovernightIndex = safe_cast<COvernightIndex^> (overnightIndex);
        _CovernightIndex->Lock();
        boost::shared_ptr<QuantLib::OvernightIndex>& _overnightIndex = static_cast<boost::shared_ptr<QuantLib::OvernightIndex>&> (_CovernightIndex->GetShared ()); 
        _ppDatedOISRateHelper = new boost::shared_ptr<QuantLib::DatedOISRateHelper> (new QuantLib::DatedOISRateHelper ( _startDate,  _endDate,  _fixedRate,  _overnightIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfixedRate != nullptr) _CfixedRate->Unlock();
        if (_CovernightIndex != nullptr) _CovernightIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (boost::shared_ptr<QuantLib::DatedOISRateHelper>& childNative, Object^ owner) : CRateHelper(CDatedOISRateHelper::typeid)
{
#ifdef HANDLE
	_phDatedOISRateHelper = NULL;
#endif
	_ppDatedOISRateHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
}
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (QuantLib::DatedOISRateHelper& childNative, Object^ owner) : CRateHelper(CDatedOISRateHelper::typeid)
{
#ifdef HANDLE
	_phDatedOISRateHelper = NULL;
#endif
	_ppDatedOISRateHelper = new boost::shared_ptr<QuantLib::DatedOISRateHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
    _DatedOISRateHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (CDatedOISRateHelper^ copy) : CRateHelper(CDatedOISRateHelper::typeid)
{
#ifdef HANDLE
	_phDatedOISRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDatedOISRateHelper = new boost::shared_ptr<QuantLib::DatedOISRateHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (System::Type^ t) : CRateHelper(CDatedOISRateHelper::typeid)
{
#ifdef HANDLE
	_phDatedOISRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(CDatedOISRateHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (QuantLib::Handle<QuantLib::DatedOISRateHelper>& childNative, Object^ owner)  : CRateHelper(CDatedOISRateHelper::typeid)
{
	_phDatedOISRateHelper = &childNative;
	_ppDatedOISRateHelper = &static_cast<boost::shared_ptr<QuantLib::DatedOISRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
    _DatedOISRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (QuantLib::Handle<QuantLib::DatedOISRateHelper> childNative)  : CRateHelper(CDatedOISRateHelper::typeid)
{
	_phDatedOISRateHelper = &childNative;
	_ppDatedOISRateHelper = &static_cast<boost::shared_ptr<QuantLib::DatedOISRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::CDatedOISRateHelper (QuantLib::DatedOISRateHelper childNative)  : CRateHelper(CDatedOISRateHelper::typeid)
{
#ifdef HANDLE
	_phDatedOISRateHelper = NULL;
#endif
	_ppDatedOISRateHelper = new boost::shared_ptr<QuantLib::DatedOISRateHelper> (new QuantLib::DatedOISRateHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDatedOISRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::~CDatedOISRateHelper ()
{
    if (_ppDatedOISRateHelper != NULL)
    {
	    delete _ppDatedOISRateHelper;
        _ppDatedOISRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::!CDatedOISRateHelper ()
{
    if (_ppDatedOISRateHelper != NULL)
    {
	    delete _ppDatedOISRateHelper;
    }
}
QuantLib::DatedOISRateHelper& Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::GetReference ()
{
    if (_ppDatedOISRateHelper == NULL) throw gcnew NativeNullException ();
	return **_ppDatedOISRateHelper;
}
boost::shared_ptr<QuantLib::DatedOISRateHelper>& Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::GetShared ()
{
    if (_ppDatedOISRateHelper == NULL) throw gcnew NativeNullException ();
	return *_ppDatedOISRateHelper;
}
QuantLib::DatedOISRateHelper* Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::GetPointer ()
{
    if (_ppDatedOISRateHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppDatedOISRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DatedOISRateHelper>& Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::GetHandle ()
{
	if (_phDatedOISRateHelper == NULL)
	{
		_phDatedOISRateHelper = new Handle<QuantLib::DatedOISRateHelper> (*_ppDatedOISRateHelper);
	}
	return *_phDatedOISRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::HasNative () 
{
	return (_ppDatedOISRateHelper != NULL);
}

Double Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDatedOISRateHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IDatedOISRateHelper^ Cephei::QL::Termstructures::Yield::CDatedOISRateHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppDatedOISRateHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IDatedOISRateHelper^ Cephei::QL::Termstructures::Yield::CDatedOISRateHelper_Factory::Create (DateTime startDate, DateTime endDate, Cephei::QL::IQuote^ fixedRate, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex)
{
    return gcnew CDatedOISRateHelper ( startDate,  endDate,  fixedRate,  overnightIndex);
}
